package com.itheima.server.imp;

import com.github.pagehelper.PageHelper;
import com.itheima.mapper.Ksmapper;
import com.itheima.pojo.Page;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.User;
import com.itheima.pojo.UserInfo;
import com.itheima.server.KsService;
import com.itheima.utils.JwtUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class KsServiceImpl implements KsService {
    @Autowired
    private Ksmapper ksmapper;
    @Override
    public PageResult<User> page1(Page page) {
        PageHelper.startPage(page.getPage(),page.getPageSize());
        List<User> List = ksmapper.list(page);
        com.github.pagehelper.Page<User> p = (com.github.pagehelper.Page<User>) List;

        return new PageResult<>(p.getTotal(),List);
    }

    @Override
    public UserInfo login(User user) {
        User u=ksmapper.login(user);
        if(u!=null){
            UserInfo userInfo=new UserInfo();
            BeanUtils.copyProperties(u,userInfo);
            Map<String, Object> map = new HashMap<>();
            map.put("username",u.getUsername());
            map.put("password",u.getPassword());
            map.put("name",u.getName());
            userInfo.setToken(JwtUtils.generateJwt(map));
            return userInfo;

        }

        return null;
    }
}
